package com.ssm.zhihu_ssm.controller;

import com.ssm.zhihu_ssm.service.UserMessageService;
import com.ssm.zhihu_ssm.service.UserService;
import com.ssm.zhihu_ssm.util.ReturnMap;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

@RestController
public class UploadController {

    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String testPicUpload(@RequestParam("file") MultipartFile[] file, String phone,String img) throws Exception {
        System.out.println("长度" + file.length);

        int id = userService.findId(phone);
        Boolean isTrue;
        if (file.length != 0) {
            try {
                //图片命名
                for (int i = 0; i < file.length; i++) {
//                    SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//                    Calendar calendar = Calendar.getInstance();
                    String newCompanyImageName = img;
//                            sFormat.format(calendar.getTime()) + ".jpg";
                    String newCompanyImagepath = "C:\\project_picture\\" + newCompanyImageName;
                    File newFile = new File(newCompanyImagepath);
                    if (!newFile.exists()) {
                        newFile.createNewFile();
                    }
                    BufferedOutputStream out = new BufferedOutputStream(
                            new FileOutputStream(newFile));
                    out.write(file[i].getBytes());
                    out.flush();
                    out.close();
                    isTrue = userMessageService.setimg(id,newCompanyImageName);

                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "图片上传失败！";
            } catch (IOException e) {
                e.printStackTrace();
                return "图片上传失败！";
            }
        }
        return "test";
    }
//    @ResponseBody
//    @RequestMapping(value = "/upload", method = RequestMethod.POST)
//    public String testPicUpload(@RequestParam("file") MultipartFile[] file, String phone) throws Exception {
//        System.out.println("长度" + file.length);
//
//        int id = userService.findId(phone);
//        Boolean isTrue;
//        if (file.length != 0) {
//            try {
//                //图片命名
//                for (int i = 0; i < file.length; i++) {
//                    SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//                    Calendar calendar = Calendar.getInstance();
//                    String newCompanyImageName = sFormat.format(calendar.getTime()) + ".jpg";
//                    String newCompanyImagepath = "C:\\project_picture\\" + newCompanyImageName;
//                    File newFile = new File(newCompanyImagepath);
//                    if (!newFile.exists()) {
//                        newFile.createNewFile();
//                    }
//                    BufferedOutputStream out = new BufferedOutputStream(
//                            new FileOutputStream(newFile));
//                    out.write(file[i].getBytes());
//                    out.flush();
//                    out.close();
//                    isTrue = userMessageService.setimg(id,newCompanyImagepath);
//
//                }
//
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//                return "图片上传失败！";
//            } catch (IOException e) {
//                e.printStackTrace();
//                return "图片上传失败！";
//            }
//        }
//        return "test";
//    }
}





